package jmine.tec.hydra.services.incluir;

import jmine.tec.database.pack.PackException;
import jmine.tec.database.pack.services.PackEntityPersistenceHelper;
import jmine.tec.di.annotation.Injected;
import jmine.tec.hydra.domain.senderRegistry.MQSenderRegistry;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.Output;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.ServiceProperties;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.api.export.ServiceFiller;
import jmine.tec.services.export.impl.ExportUtils;
import jmine.tec.services.io.ServiceBean;

/**
 * Serviço de inclusão de um SenderRegistry que envia mensagens via JMS
 * 
 * @author lundberg
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class MQSenderRegistryService implements ServiceFiller<MQSenderRegistry> {

    public static final String IDENTIFICADOR = "Identificador";

    public static final String MNEMONICO = "Mnemônico";

    public static final String QUEUE = "Fila";

    public static final String QUEUE_CONNECTION_FACTORY = "Fabrica de Conexões";

    public static final String DATABASE_ID = "Database ID";

    // Injected
    private PackEntityPersistenceHelper helper;

    private ServiceProperties serviceProperties;

    // Obrigatórios
    private String mnemonico;

    // Opcional
    private Long databaseID;

    /**
     * Registra um novo sender para JMS.
     * 
     * @return MQSenderRegistry
     * @throws PackException PackException
     */
    @Execution
    @Output(propertyName = IDENTIFICADOR)
    public MQSenderRegistry execute() throws PackException {
        MQSenderRegistry registry = this.helper.findOrCreate(MQSenderRegistry.class, this.databaseID, this.mnemonico);
        registry.setMnemonico(this.mnemonico);
        registry.setQueue(this.serviceProperties.getRaw(QUEUE));
        registry.setQueueConnectionFactory(this.serviceProperties.getRaw(QUEUE_CONNECTION_FACTORY));
        this.helper.save(registry, this.databaseID);
        return registry;
    }

    /**
     * {@inheritDoc}
     */
    public void fillServiceBean(ServiceBean bean, ReferenceMap referenceMap, MQSenderRegistry entity) {
        ExportUtils.setService(bean, this.getClass());
        ExportUtils.put(bean, IDENTIFICADOR, referenceMap.addReference(entity, "jmsSenderRegistry"));
        ExportUtils.put(bean, MNEMONICO, entity.getMnemonico());
        ExportUtils.put(bean, QUEUE, entity.getQueue());
        ExportUtils.put(bean, QUEUE_CONNECTION_FACTORY, entity.getQueueConnectionFactory());
        ExportUtils.putDatabaseId(bean, DATABASE_ID, entity);
    }

    /**
     * @param helper the helper to set
     */
    @Injected
    public void setHelper(PackEntityPersistenceHelper helper) {
        this.helper = helper;
    }

    /**
     * @param serviceProperties the serviceProperties to set
     */
    @Injected
    public void setServiceProperties(ServiceProperties serviceProperties) {
        this.serviceProperties = serviceProperties;
    }

    /**
     * @param mnemonico the mnemonico to set
     */
    @Input(fieldName = MNEMONICO)
    public void setMnemonico(String mnemonico) {
        this.mnemonico = mnemonico;
    }

    /**
     * @param queue the queue to set
     */
    @Input(fieldName = QUEUE)
    public void setQueue(String queue) {
    }

    /**
     * @param queueConnectionFactory the queueConnectionFactory to set
     */
    @Input(fieldName = QUEUE_CONNECTION_FACTORY)
    public void setQueueConnectionFactory(String queueConnectionFactory) {
    }

    /**
     * @param databaseID the databaseID to set
     */
    @Input(fieldName = DATABASE_ID, required = false)
    public void setDatabaseID(Long databaseID) {
        this.databaseID = databaseID;
    }
}
